package com.yuhong.springbootinit.job.cycle;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuhong.springbootinit.mapper.ApplicationMapper;
import com.yuhong.springbootinit.mapper.InterviewMapper;
import com.yuhong.springbootinit.model.entity.Interview;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: resume-analysis-system
 * @BelongsPackage: com.yuhong.springbootinit.job.cycle
 * @Author: renyuhong
 * @CreateTime: 2025-05-07  15:18
 * @Description:
 * @Version: 1.0
 */
@Component
@Slf4j
public class UpdateInterviewStatusTask {

    @Resource
    private InterviewMapper interviewMapper;


    /**
     * 每 1 分钟执行一次，将状态为 1/3/5 的 Interview 自动推进到下一个阶段
     */
    @Scheduled(fixedRate = 60 * 1000)
    public void updateInterviewStatus() {
        // 要更新的状态及其对应的新状态
        List<Integer> currentStatuses = Arrays.asList(1, 3, 5);

        QueryWrapper<Interview> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status", currentStatuses);
        List<Interview> interviews = interviewMapper.selectList(queryWrapper);

        if (interviews == null || interviews.isEmpty()) {
            log.info("没有需要更新的面试状态记录");
            return;
        }

        Date now = new Date();

        for (Interview interview : interviews) {
            Integer currentStatus = interview.getStatus();
            switch (currentStatus) {
                case 1:
                    interview.setStatus(2); // 已发起 → 测评中
                    interview.setTestDate(now);
                    break;
                case 3:
                    interview.setStatus(4); // 测评完成 → 笔试中
                    interview.setExamDate(now);
                    break;
                case 5:
                    interview.setStatus(6); // 笔试完成 → 面试中
                    interview.setInterviewDate(now);
                    break;

                default:
                    continue;
            }

            interviewMapper.updateById(interview);
            log.info("已更新面试记录 id={}，新状态={}", interview.getId(), interview.getStatus());
        }

        log.info("定时任务结束，共更新 {} 条记录", interviews.size());
    }
}
